package com.blb.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.blb.springboot.entity.Log;
import com.blb.springboot.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.List;

/**
 * @author lvtian
 * @date 2023/1/5 19:55
 */
@Controller
public class LogController {

    @Autowired
    private LogService logService;
    @RequestMapping("/getAll")
    public String getAll(@RequestParam( value = "current",required = false,defaultValue = "1") Integer current,Model model){
        Page<Log> page = new Page<>(current, 2);
        //执行分页查询
        logService.getAll(page);
        List<Log> logistics = page.getRecords();
        model.addAttribute("logistics",logistics);
        model.addAttribute("page",page);
        return "menu";
    }


    @RequestMapping("/findLogById")
    public String findQuestionSetById(Integer id ,Model model) throws ServletException, IOException {
        Log log = logService.getById(id);
        model.addAttribute("log",log);
        //jsp还没写
        return "updateLog";
    }

    @RequestMapping("/removeLog")
    public String remove(Integer id ,Model model) throws ServletException, IOException {
        logService.removeById(id);
        return "redirect:/getAll";
    }

    //进入添加页面
    @RequestMapping("/toAddLog")
    public String toAddLog(){
        return "addLogMenu";
    }

    //添加
    @RequestMapping("/addLog")
    public String addLog(Log log) throws ServletException, IOException {
        System.out.println(log);
        logService.save(log);
        return "redirect:/getAll";
    }

    //修改
    @RequestMapping("/updateLog")
    public String updateLog(Log log) throws ServletException, IOException {
        logService.updateById(log);
        return "redirect:/getAll";
    }
}
